package DCRS::Admin;
#----------------------------------------------------------------------------
# Name:         Call.pm
# Purpose:      Process DCRS Clarion database files for call reports
# 
# Notes:        This module is part of the DCRS Dispatch Web Interface 
#
# Author:       Kingsley Wright
#
# Created:      2007-2008
# Copyright:    (c) 2008 by DCRS Solutions, Inc.
# Licence:      TBD - All rights reversed, DCRS Solutions, Inc.
#----------------------------------------------------------------------------
use strict;
use warnings;
use base 'CGI::Application';
use DBI;
use CGI::Lite;

my $DBPATH = '/media/sys/PUBLIC/OFFICE4/';
my $localtime = localtime();
my @pwd = ();
my @main_loop = ();

sub setup 
{
    my $self = shift;
    $self->start_mode('start');
    $self->run_modes(
        all  => 'show_admin_menu',
        sonicwall => 'show_sonicwall_pwd_list',
        nsm_keys => 'show_nsm_keys'
    );
}

sub show_admin_menu 
{
    my $self = shift;
    my $q = $self->query();
    my $title = "DCRS Admin Menu";
    
    my $template = $self->load_tmpl(
        'admin/all.tmpl',
        global_vars       => 1,
        loop_context_vars => 1,
        die_on_bad_params => 0,
    );
                                    
    $template->param(localtime => $localtime,);     
    return $template->output();
  
}

sub show_sonicwall_pwd_list 
{
    my $self = shift;
    my $q = $self->query();
    my $title = "DCRS Password List";
    my $template = $self->load_tmpl(
        'admin/getpwd.tmpl',
        global_vars => 1,
        loop_context_vars => 1,
        die_on_bad_params => 0,
    );
                                
    get_sonicwall_passwords();
    make_loop_sonicwall();
    
    # create template variables....
    $template->param( main_loop => \@main_loop );
    $template->param( title => $title );
    
    return  $template->output;
}

sub make_loop_sonicwall
{
    my ($site_id, $site_name, $sw_user, $sw_pass, $su_pass, );
    
    for my $pwd_ref ( sort { $a->[2] cmp $b->[2] } @pwd ) {
    
    my %row={};
        
    $site_id	= join("-", @$pwd_ref[0], @$pwd_ref[1]);
    $site_name	= @$pwd_ref[2];
    $sw_user	= @$pwd_ref[3];
    $sw_pass	= @$pwd_ref[6];
    $su_pass	= @$pwd_ref[8]; 
    
    #fill the template variables 
    $row {site_id}	 = $site_id;
    $row {site_name} = $site_name;
    $row {sw_user}	 = $sw_user;
    $row {sw_pass}	 = $sw_pass;
    $row {su_pass}	 = $su_pass;
    
    push (@main_loop, \%row) 	
    }
}

sub get_sonicwall_passwords
{          
    #open connection to Access database
    my $dbh = DBI->connect('dbi:ODBC:MDBDSN');

    #prepare and execute SQL statement
    my $sqlstatement = "SELECT * FROM Passwords";
    my $sth = $dbh->prepare($sqlstatement);
    $sth->execute || 
        die "Could not execute SQL statement ... maybe invalid?";
    
    #output database results
    while (my @row=$sth->fetchrow_array()){ 
                      
    #   if ($row[2] =~ m/$s/){	
    #       push @pwd, [@row];
    #   }
        push @pwd, [@row];
    }
}

sub show_nsm_keys 
{
    my $self = shift;
    my $q = $self->query();
    my $title = "NSM Gateway Keys";
    my $template = $self->load_tmpl(
        'admin/getnsm.tmpl',
        global_vars => 1,
        loop_context_vars => 1,
        die_on_bad_params => 0,
    );
                                
    get_nsm_keys();
    make_loop_nsm();
    
    # create template variables....
    $template->param( main_loop => \@main_loop );
    $template->param( title => $title );
    
    return  $template->output;
} 

sub get_nsm_keys
{          
    #open connection to Access database
    my $dbh = DBI->connect('dbi:ODBC:MDBDSN');

    #prepare and execute SQL statement
    my $sqlstatement = "SELECT * FROM Passwords";
    my $sth = $dbh->prepare($sqlstatement);
    $sth->execute || 
        die "Could not execute SQL statement ... maybe invalid?";
    
    #output database results
    while (my @row=$sth->fetchrow_array()){ 
                      
    #   if ($row[2] =~ m/$s/){	
    #       push @pwd, [@row];
    #   }
        push @pwd, [@row];
    }
}

sub make_loop_nsm
{
    my ($site_id, $site_name, $sw_user, $sw_pass, $su_pass, );
    
    for my $pwd_ref ( sort { $a->[2] cmp $b->[2] } @pwd ) {
    
    my %row={};
        
    $site_id	= join("-", @$pwd_ref[0], @$pwd_ref[1]);
    $site_name	= @$pwd_ref[2];
    $sw_user	= @$pwd_ref[3];
    $sw_pass	= @$pwd_ref[6];
    $su_pass	= @$pwd_ref[8]; 
    
    #fill the template variables 
    $row {site_id}	 = $site_id;
    $row {site_name} = $site_name;
    $row {sw_user}	 = $sw_user;
    $row {sw_pass}	 = $sw_pass;
    $row {su_pass}	 = $su_pass;
    
    push (@main_loop, \%row) 	
    }
}
1;